// pages/map/map.js
let QQMapWX = require('../../libs/qqmap-wx-jssdk')
let qqmapsdk;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchKey:"",
    myLat: 35.55396,
    myLon: 114.316,
    myAddress:'',
    showType: "0",
    // 如果showDistance=0即视为未选择
    showDistance: "4",
    showDate: "4",
    showFilter: false,
    filterChoose: 'date',
    isDateChoosed: false,
    isLocationChoosed: false,
    isCategoryChoosed: false,
    markers:[
      {
        id: 1,
        latitude: 30.55396,
        longitude: 114.317,
      },
      {
        id: 2,
        latitude: 30.55496,
        longitude: 114.31590,
      },
      {
        id: 3,
        latitude: 30.5559,
        longitude: 114.3155,
      },
      {
        id: 4,
        latitude: 30.5529,
        longitude: 114.3155,
      },
      {
        id: 5,
        latitude: 30.5559,
        longitude: 114.3195,
      },
      {
        id: 6,
        latitude: 30.5529,
        longitude: 114.3175,
      }
    ],
    typeList:['失物招领', '寻物启事', '求跑腿','物品供需'],
    dateList:['今日内','最近三天', '最近五天', '最近七天'],
    distanceList:[100, 300, 500, 1000]
  },

  getLocation() {
    console.log("in")
    qqmapsdk = new QQMapWX({
        key: 'RYKBZ-4C36R-CNJWK-W7ND5-HXB77-OXBJI' //之前在腾讯平台申请到的key
    })
    let that = this;

    wx.getLocation({ //获取定位地址经纬度
        isHighAccuracy: true,
        type: 'gcj02',
        success(res) {
          console.log(res);
          that.setData({
            "myLat": res.latitude,
            "myLon": res.longitude
          })
          console.log("out")
          const latitude = res.latitude
          const longitude = res.longitude
          qqmapsdk.reverseGeocoder({ //SDK调用
            location: {
              latitude,
              longitude
            },
            success: (res) => {
              console.log("return:",res.result.address);
              that.setData({
                myAddress: res.result.address
              })
            }
          })
        }
    })
},

  showFilter(){
    if(this.data.showFilter){
      this.setData({
        showFilter: false
      })
    }else{
      this.setData({
        showFilter: true
      })
    }
},

showDetailFilter(e){
  const type = e.currentTarget.dataset.id;
  let that = this;
  console.log(type);
  switch(type){
    case "0":
      console.log("time");
      wx.showActionSheet({
        itemList: ['今日内','最近三天', '最近五天', '最近七天'],
        success (res) {
          that.setData({
            "showDate": res.tapIndex,
            "filterChoose": 'date',
            "isDateChoosed": true
          })
        },
        fail (res) {
          console.log(res.errMsg)
        }
      })
      break;
    case "1":
      console.log("location");
      wx.showActionSheet({
        itemList: ['当前位置为'+that.data.myAddress,'100米内', '300米内', '500米内','1000米内'],
        success (res) {
          if (res.tapIndex != 0) {
            that.setData({
              "showDistance": res.tapIndex,
              "filterChoose": 'location',
              "isLocationChoosed": true
            })
          }
          // if (res.tapIndex == 2) {
          //   that.setData({
          //     markers:[
          //       {
          //         id: 1,
          //         latitude: 30.55396,
          //         longitude: 114.317,
          //       },
          //       {
          //         id: 2,
          //         latitude: 30.55496,
          //         longitude: 114.31590,
          //       },
          //       {
          //         id: 4,
          //         latitude: 30.5529,
          //         longitude: 114.3155,
          //       },
          //     ]
          //   })
          // }
        },
        fail (res) {
          console.log(res.errMsg)
        }
      })
      break;
    case "2":
      console.log("category");
      wx.showActionSheet({
        itemList: ['失物招领', '寻物启事', '求跑腿','物品供需'],
        success (res) {
          that.setData({
            "showType": res.tapIndex,
            "filterChoose": 'category',
            "isCategoryChoosed": true
          })
        },
        fail (res) {
          console.log(res.errMsg)
        }
      })
      break;
   }
},

search(){
  console.log("type", this.data.typeList[this.data.showType])
  console.log("key", this.data.searchKey)
  console.log("date", this.data.dateList[this.data.showDate])
  console.log("x", this.data.myLon)
  console.log("y", this.data.myLat)
  console.log("distance", this.data.distanceList[this.data.showDistance-1])
  let that = this;
  wx.request({
    url: 'http://192.168.43.196:8080/map/search?type='+that.data.typeList[that.data.showType]+"&key="+that.data.searchKey+"&date="+that.data.dateList[that.data.showDate]+"&x="+that.data.myLon+"&y="+that.data.myLat+"&distance="+that.data.distanceList[that.data.showDistance-1],
    method: "GET",
    header: {
      'content-type': 'application/json' 
    },
    // data: {
    //   type: that.data.typeList[that.data.showType],
    //   key: that.data.searchKey,
    //   date: that.data.dateList[that.data.showDate],
    //   x: that.data.myLon,
    //   y: that.data.myLat,
    //   distance: that.data.distanceList[that.data.showDistance]
    // },
    success(res){
      console.log("return",res)
    }
  })
},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },


})
